#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>

#define filename "log.txt"

int main()
{
  int fd = open(filename,O_CREAT|O_WRONLY|O_APPEND,0666);
  if(fd < 0)
  {
    perror("open");
    return 1;

  }
  printf("fd:%d\n",fd);

  printf("hello world\n");
  fprintf(stdout,"hello linux\n");
  char inbuffer[1024];
  ssize_t s = read(0,inbuffer,sizeof(inbuffer) - 1);//0表示从键盘读入，s保存的读取的字节数大小
  if(s > 0)
  {
    inbuffer[s]= '\0';
    printf("echo# %s\n",inbuffer);

  }
  //重定向
  dup2(fd,1);//让1地址指向fd地址处
  close(fd);
  printf("fd:%d\n",fd);

  const char*msg  = "hello linux\n";
  int cnt = 5;
  while(cnt)
  {
    write(1,msg,strlen(msg));
    cnt--;
  }//这样他就写到文件里了
  return 0;

}
